import { io } from "socket.io-client";

const socket = io('wss://localhost:3666', {
    pingInterval: 10000,
    pingTimeout: 5000,
    reconnection: true,  // 确保启用自动重连
    reconnectionAttempts: 5, // 重试次数
    reconnectionDelay: 1000, // 每次重连的延迟
    query: {
        token: localStorage.getItem('accessToken')  // 从本地存储获取 JWT 令牌
    },
    transports: ['websocket'],  // 强制使用 WebSocket
    autoConnect: false,        // 不自动连接，手动控制
    compress: true,            // 启用压缩
});


socket.on('connect', () => {
    const user = JSON.parse(sessionStorage.getItem('user'));
    if (user) {
        socket.emit('login', user._id); // 登录时发送用户ID
        console.log("Socket 连接成功，SocketID:", socket.id);
    }
});

socket.connect();

export default socket;
